package com.yaolisheng.poetryandode.controller;

import com.yaolisheng.poetryandode.common.utils.UploadUtil;
import com.yaolisheng.poetryandode.entity.TBlog;
import com.yaolisheng.poetryandode.service.TBlogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;

/**
 * @author yaolisheng
 * @version 1.0
 * @date 2021/6/8 22:32
 */

@Controller
@RequestMapping("/input")
public class BlogInputController {

    @Autowired
    private TBlogService blogService;

    @RequestMapping("/input")
    public String blogInput(){

        return "admin/blogs-input";
    }

    @RequestMapping("/blogInputSave")
    public String blogInputSave(TBlog blog){

        blogService.insert(blog);

        return "chengfgong";

    }

    @RequestMapping("/list")
    public String list(){

        return "admin/blogs";
    }


    //处理文件上传
    @RequestMapping(value="/testuploadimg", method = RequestMethod.POST)
    @ResponseBody
    public String uploadImg(@RequestParam("file") MultipartFile file,
                     HttpServletRequest request) {
        String contentType = file.getContentType();
        String fileName = file.getOriginalFilename();
//        String filePath = request.getSession().getServletContext().getRealPath("imgupload/");
        String filePath = "/pdf";
        try {
            UploadUtil.uploadFile(file.getBytes(), filePath, fileName);
        } catch (Exception e) {
            // TODO: handle exception
        }
        //返回json
        return "uploadimg success";
    }


}
